package aframe.manage;


import aframe.utils.AppUtils;
import aframe.utils.LogUtils;
import aframe.utils.Md5Utils;
import aframe.utils.SDCardUtils;
import aframe.utils.StringUtils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.DisplayMetrics;

public class ManageApp {
	/**
	 * designWidth os 这两个参数可能需要更改
	 */

	private static int screenWidth = -1;
	private static int screenHight = -1;
	private static float density = -1;
	private static String clientId;

	// 缓存目录
	private static String imageFileDir = "";
	private static String appDataDir = "";

	private static int versionCode = -1;
	private static String versionName = "";

	public static int getDesignWidth() {
		return 640;
	}

	public static String getOs() {
		return "android";
	}

	public static String getClientId() {
		return clientId;
	}

	public static void setClientId(String clientId) {
		ManageApp.clientId = clientId;
	}

	public static int getSreenWidth(Context context) {
		if (screenWidth < 0) {
			screenWidth = getDm(context).widthPixels;
		}
		return screenWidth;
	}

	public static int getScreenHight(Context context) {
		if (screenHight < 0) {
			screenHight = getDm(context).heightPixels;
		}
		return screenHight;
	}

	public static float getDensity(Context context) {
		if (density < 0) {
			density = getDm(context).density;
		}
		return density;
	}

	private static DisplayMetrics getDm(Context context) {
		return AppUtils.getDisplayMetrics(context);
	}

	// 获取数据缓存目录
	public static String getAppDataDir(Context context) {
		if (strIsEmpty(appDataDir)) {
			if (sdCardCanUse()) {
				appDataDir = getSdCardDir(context) + "/file/" + Md5Utils.MD5("app") + "/";
			} else {
				appDataDir = getCacheDir(context) + "/" + Md5Utils.MD5("app") + "/";
			}
		}
		return appDataDir;
	}

	// 获取图片缓存目录
	public static String getImageFileDir(Context context) {
		if (strIsEmpty(imageFileDir)) {
			if (sdCardCanUse()) {
				imageFileDir = getSdCardDir(context) + "/file/image/";
			} else {
				imageFileDir = getFileDir(context) + "/image/";
			}
		}
		return imageFileDir;
	}

	public static int getVersionCode(Context context) {
		if (versionCode < 0) {
			PackageInfo packagetInfo = getPackagetInfo(context);
			if (packagetInfo != null) {
				versionCode = packagetInfo.versionCode;
			}
		}
		return versionCode;

	}

	public static String getVersionName(Context context) {
		if (strIsEmpty(versionName)) {
			PackageInfo packagetInfo = getPackagetInfo(context);
			if (packagetInfo != null) {
				versionName = packagetInfo.versionName;
			}
		}
		return versionName;
	}

	private static PackageInfo getPackagetInfo(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			LogUtils.exception(e);
			return null;
		}
	}

	private static boolean strIsEmpty(String str) {
		return StringUtils.isEmpty(str);
	}

	private static String getCacheDir(Context context) {
		return AppUtils.getCacheDirAbsolutePath(context);
	}

	private static String getFileDir(Context context) {
		return AppUtils.getFileDirAbsolutePath(context);
	}

	private static boolean sdCardCanUse() {
		return SDCardUtils.sdCardCanUse();
	}

	private static String getSdCardDir(Context context) {
		return SDCardUtils.getSdCardAbsolutePath() + "/" + getAppName(context);
	}

	private static String getAppName(Context context) {
		return AppUtils.getAppName(context);
	}
}
